<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="ajax.jsp"%>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用后台管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        fieldset {
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-top: 20px;
            padding: 10px;
        }

        legend {
            font-weight: bold;
        }

        .form {
            margin: 20px auto;
            max-width: 600px;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-top: 5px;
        }

        .btn {
            padding: 8px 16px;
            margin-top: 10px;
            border: none;
            border-radius: 5px;
            text-decoration: none;
            color: #fff;
            background-color: #007bff;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<script>
    function addAdmin(url){
        let username = document.getElementById('username').value;
        let realName = document.getElementById('realName').value;
        let password = document.getElementById('password').value;
        let contact = document.getElementById('contact').value;
        let admin = {
            username: username,
            password: password,
            realName: realName,
            contact: contact
        }
        axiosInstance.post(
            url,
            admin,
            {
                headers: headersJson
            }
        ).then((res)=>{
            console.log(res.data);
            if (res.data.code == 200){
                console.log("增加成功");
                document.getElementById('btn-msg').innerHTML = "增加成功";
                setTimeout(()=>{
                    document.getElementById('btn-msg').innerHTML = "";
                    location.reload();
                }, 2000);
            }else {
                console.log("增加失败");
                document.getElementById('btn-msg').innerHTML = "增加失败";
                setTimeout(()=>{
                    document.getElementById('btn-msg').innerHTML = "";
                }, 2000);
            }
        }).catch(e=>{
            console.log(e);
            console.log("增加出现异常");
            document.getElementById('btn-msg').innerHTML = "增加出现异常";
            setTimeout(()=>{
                document.getElementById('btn-msg').innerHTML = "";
            }, 2000);
        })
    }
</script>
<div class="form">
    <fieldset>
        <legend>新增管理员信息</legend>
        <form id="addForm" name="myform">
            <div class="form-item">
                <label for="username">用户名</label>
                <input type="text" name="username" id="username" placeholder="请输入用户名" required/>
            </div>
            <div class="form-item">
                <label for="password">密码</label>
                <input type="password" name="password" id="password" placeholder="请输入密码" required/>
            </div>
            <div class="form-item">
                <label for="realName">姓名</label>
                <input type="text" name="realName" id="realName" placeholder="请输入姓名" required/>
            </div>
            <div class="form-item">
                <label for="contact">联系方式</label>
                <input type="text" name="contact" id="contact" placeholder="请输入联系方式"/>
            </div>
            <div class="form-item">
                <button class="btn" type="button" onclick="addAdmin('/admin/addAdmin')" id="sub">立即提交</button>
                <button type="reset" class="btn" id="res">取消重置</button>
            </div>
            <div id="btn-msg"></div>
        </form>
    </fieldset>
</div>
</body>
</html>
